package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.*;
import cn.wolfcode.crm.mapper.*;
import cn.wolfcode.crm.page.PageResult;
import cn.wolfcode.crm.query.AccidentQueryObject;
import cn.wolfcode.crm.service.IAccidentService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AccidentServiceImpl implements IAccidentService {

    @Autowired
    private AccidentMapper accidentMapper;
    @Autowired
    private LossMapper lossMapper;
    @Autowired
    private CasesMapper casesMapper;
    @Autowired
    private LossItemMapper lossItemMapper;
    @Autowired
    private PolicyMapper policyMapper;
    @Autowired
    private ProductMapper productMapper;
    @Override
    public PageResult query(AccidentQueryObject qo) {
       Integer total = accidentMapper.queryForCount(qo);
       if (total==0){
           return new PageResult();
       }
        List<Accident> rows = accidentMapper.queryForList(qo);
        return new PageResult(total, rows);
    }

    @Override
    public void save(Accident accident) {
        Employee employee = (Employee) SecurityUtils.getSubject().getPrincipal();
        accident.setEmployee(employee);
        accidentMapper.insert(accident);
        //改变报案表Cases的状态为已处理
       casesMapper.changeState(accident.getCases().getId(), Cases.STATE_DONE);
       //更新lossItem
        Loss loss = lossMapper.selectByPrimaryKey(accident.getLossId());
        if (loss!=null){
            LossItem lossItem = lossItemMapper.selectByPrimaryKey(loss.getLossItemId());
            lossItem.setResponsibility(accident.getResponsibility());
            lossItem.setAccidentTime(accident.getDealDate());
            lossItem.setCasesDescription(accident.getCasesDescription());
            lossItemMapper.updatePartLossItemOfAccident(lossItem);
        }
    }

    @Override
    public void update(Accident accident) {

    }

    @Override
    public void handlerAccident(Long id, Long stateId) {
        accidentMapper.handlerAccident(id,stateId);
    }

    @Override
    public Loss getLossInfo(Long accidentId) {
        return lossMapper.selectByPrimaryKey(accidentId);
    }

    @Override
    public void saveAccidentLoss(Loss loss,Long accidentId) {
        if (loss.getId()==null){//保存赔损单
            lossMapper.insert(loss);
            accidentMapper.insertLossId(loss.getId(),accidentId);
            //新建赔损明细信息
            LossItem lossItem = new LossItem();
            lossItem.setBankcard(loss.getBankcard());
            lossItem.setTotalAccount(loss.getTotalAccount());
            lossItem.setTotalNumber(loss.getTotalNumber());
            lossItem.setPolicySn(loss.getPolicySn());
            //根据Sn获取policy
            Policy policy = policyMapper.getPolicyBySn(loss.getPolicySn());
            policy.getSafetyMechanism();
            lossItem.setPolicyCompany(policy.getSafetyMechanism().getName());
            lossItem.setCarName(policy.getCar().getBrand());
            lossItem.setOwnerName(policy.getCustomer().getName());
            //根据policy查询种类名即为product名
            List<Product> products = productMapper.selectByPolicyId(policy.getId());
            for (Product product : products) {
                if (product.getSafetyMechanism().getId()==policy.getSafetyMechanism().getId()){
                    lossItem.setPolicyName(product.getName());//设置保险种类名
                    break;
                }
            }
            Accident accident = accidentMapper.selectByPrimaryKey(accidentId);
            lossItem.setAccidentTime(accident.getDealDate());
            lossItem.setAccidentEmpName(accident.getEmployee().getUserName());
            lossItem.setCasesDescription(accident.getCasesDescription());
            lossItem.setResponsibility(accident.getResponsibility());
            //获取对应Cases
            lossItem.setLossAddress(accident.getCases().getAddress());
            lossItem.setCasesEmpName(accident.getCases().getEmployee().getUserName());
            lossItem.setCasesTime(accident.getCases().getReceiveTime());
            //插入
            lossItemMapper.insert(lossItem);
            //在loss插入lossItemId
            lossMapper.insertLossItemId(loss.getId(),lossItem.getId());
        }else {//更新赔损单
            lossMapper.updateByPrimaryKey(loss);
            if (loss.getLossItemId()!=null){
                LossItem lossItem = lossItemMapper.selectByPrimaryKey(loss.getLossItemId());
                lossItem.setBankcard(loss.getBankcard());
                lossItem.setTotalAccount(loss.getTotalAccount());
                lossItem.setTotalNumber(loss.getTotalNumber());
                lossItem.setPolicySn(loss.getPolicySn());
                lossItem.setOwnerName(loss.getName());
                //更新
                lossItemMapper.updatePartLossItem(lossItem);

            }
        }
    }

    @Override
    public PageResult queryForAudit(AccidentQueryObject qo) {
        Integer total = accidentMapper.auditForCount(qo);
        if (total==0){
            return new PageResult();
        }
        List<Accident> rows = accidentMapper.auditForList(qo);
        return new PageResult(total, rows);
    }
}
